package com.jye.cloud.user.service.impl;

import com.jye.cloud.domain.Record;
import com.jye.cloud.user.mapper.GroupMapper;
import com.jye.cloud.user.mapper.RecordMapper;
import com.jye.cloud.user.service.RecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class RecordServiceImpl implements RecordService {

    @Autowired
    RecordMapper recordMapper;

    @Autowired
    GroupMapper groupMapper;

    @Override
    public Record findRecordById(int id) {
        Record record = recordMapper.findRecordById(id);
        return record;
    }

    @Override
    public List<Record> findMyRecordByUId(int uId) {
        List<Record>  records = recordMapper.findMyRecordByUId(uId);
        for (Record record : records) {
            record.setExamname(groupMapper.findNameById(record.getPId()));
        }
        return records;
    }

    @Override
    public List<Record> findMyRecordByUIdLimit(int uId, Integer skip, Integer limit) {
        List<Record>  records = recordMapper.findMyRecordByUIdLimit(uId,skip,limit);
        for (Record record : records) {
            record.setExamname(groupMapper.findNameById(record.getPId()));
        }
        return records;
    }

    @Override
    public int addRecord(Record record) {
        try {
            record.setCreateTime(new Date());
            int i = recordMapper.addRecord(record);
            return record.getId();
        }catch (Exception e){
            System.out.println(e);
            return 0;
        }
    }

    @Override
    public Integer isExit(Integer uId, Integer pId) {
        return recordMapper.isExit(uId,pId);
    }

    @Override
    public List<Record> findRecordByPId(Integer pId) {
        return recordMapper.findRecordByPId(pId);
    }


}
